﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace DARE
{
    public class CBoundingRectangle: ABoundingElement
    {

        #region fields

        private Rectangle m_rect; 

        #endregion

        #region properties

        public Rectangle Rectangle { get { return m_rect; } set { m_rect = value; } }
        
        #endregion

        #region ctor

        public CBoundingRectangle(string name, CRender2D render2D)
            : this(name, render2D, null)
        {

        }

        public CBoundingRectangle(string name, CRender2D render2D, AEntity parent)
            : base(name, render2D, parent)
        {

        }


        #endregion

        #region inherited

        public override bool Contains(CBoundingCircle element)
        {
            if (Math.Abs(element.Center.Y - m_rect.Top) <= element.Ray &&
                Math.Abs(element.Center.Y - m_rect.Bottom) <= element.Ray &&
                Math.Abs(element.Center.X - m_rect.Left) <= element.Ray &&
                Math.Abs(element.Center.X - m_rect.Right) <= element.Ray)
                return true;
            return false;
        }

        public override bool Contains(CBoundingRectangle element)
        {
            return m_rect.Contains(element.Rectangle);
        }

        public override bool Intersects(CBoundingCircle element)
        {
            if (Math.Abs(element.Center.Y - m_rect.Top) > element.Ray && Math.Abs(element.Center.Y - m_rect.Bottom) > element.Ray)
                return false;
            if (Math.Abs(element.Center.X - m_rect.Left) > element.Ray && Math.Abs(element.Center.X - m_rect.Right) > element.Ray)
                return false;
            return true;
        }

        public override bool Intersects(CBoundingRectangle element)
        {
            return m_rect.Intersects(element.m_rect);
        }

        #endregion


        #region methods

        public override AEntity Clone(string name)
        {
            return Clone(name, null);
        }

        public override AEntity Clone(string name, AEntity parent)
        {
            CBoundingRectangle rect = new CBoundingRectangle(name, m_render);
            rect.Parent = parent;
            rect.m_children = new Dictionary<string, AEntity>();
            foreach (AEntity child in m_children.Values)
                rect.m_children.Add(child.Name, child.Clone(child.Name, rect));
            rect.Position = m_position;
            return rect;
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            throw new NotImplementedException();
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
